﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RMS.Model;
using RMS.View;

namespace RMS.Controller
{
    public class ResearchProjectController
    {
        private List<ResearchProject> _model;
        private ResearchProjectView _view;

        public ResearchProjectController(ResearchProjectView view)
        {
            this._model = new List<ResearchProject>();
            this._view = view;
        }

        public List<ResearchProject> Model
        {
            get { return this._model; }
            set { this._model = value; }
        }
        public ResearchProjectView View
        {
            get { return this._view; }
            set { this._view = value; }
        }

        public void InitListOfProjects()
        {
            this._model = Persistence.DataManager.Instance.SyncResearchProjects();
        }

        public void HandleAddProjectButtonClick()
        {
            if (this._view.TextBoxProjectName == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty project name");
#else
            MessageBox.Show("empty project name");
            return;
#endif
            }
            if (this._view.TextBoxTeamName == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty team name");
#else
                MessageBox.Show("empty project name");
                return;
#endif
            }
            ResearchProject project = new ResearchProject(0, this._view.TextBoxProjectName, this._view.TextBoxProjectDescription);
            project.Team = new ResearchTeam(0, this._view.TextBoxTeamName, this._view.TextBoxTeamDescription);
            this.AddProject(project);
        }

        private void AddProject(ResearchProject project)
        {
            int id = Persistence.DataManager.Instance.AddResearchProject(project);
            if (id > 0)
            {
                project.Id = id;
                int teamId = Persistence.DataManager.Instance.GetTeamID(project.Id);
                if (teamId > 0)
                {
                    project.Team.Id = teamId;
                    this.Model.Add(project);
                    this.View.PopulateProjectList(this.Model);
                }
            }
        }

        public void HandleRemoveProjectButtonClick()
        {
            if (this._view.TextBoxProjectId == "")
                return;
            ResearchProject project = new ResearchProject(Convert.ToInt32(this._view.TextBoxProjectId), "", "");
            this.RemoveProject(project);
        }

        private void RemoveProject(ResearchProject project)
        {
            int retcode = Persistence.DataManager.Instance.RemoveResearchProject(project);
            this.Model.Remove(project);
            this._view.PopulateProjectList(this._model);
        }

        public void HandleUpdateProjectButtonClick()
        {
            if (this._view.TextBoxProjectName == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty project name");
#else
            MessageBox.Show("empty project name");
            return;
#endif
            }
            if (this._view.TextBoxTeamName == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty team name");
#else
                MessageBox.Show("empty project name");
                return;
#endif
            }
            if (this._view.TextBoxProjectId == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty project id");
#else
                MessageBox.Show("empty project name");
                return;
#endif
            }
            if (this._view.TextBoxTeamId == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty team id");
#else
                MessageBox.Show("empty project name");
                return;
#endif
            }
            ResearchProject project = new ResearchProject(Convert.ToInt32(this._view.TextBoxProjectId), this._view.TextBoxProjectName, this._view.TextBoxProjectDescription);
            project.Team = new ResearchTeam(Convert.ToInt32(this._view.TextBoxTeamId), this._view.TextBoxTeamName, this._view.TextBoxTeamDescription);
            this.UpdateProject(project);
        }

        private void UpdateProject(ResearchProject project)
        {
            if (project == null) return;
            Persistence.DataManager.Instance.UpdateResearchProject(project);
            foreach (ResearchProject currentProject in this.Model)
            {
                if (currentProject.Id == project.Id)
                {
                    currentProject.Name = project.Name;
                    currentProject.Description = project.Description;
                    currentProject.Team.Name = project.Team.Name;
                    currentProject.Team.Description = project.Team.Description;
                    this.View.PopulateProjectList(this.Model);
                    break;
                }
            }
        }

        public void HandleSearchProjectButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleCancelSearchProjectButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleAddPhaseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleRemovePhaseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleUpdatePhaseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleSearchPhaseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleCancelSearchPhaseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleAddActivityButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleRemoveActivityButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleUpdateActivityButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleSearchActivityButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleCancelSearchActivityButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleAddExpenseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleRemoveExpenseButtonClick()
        {
            throw new NotImplementedException();
        }

        public bool HandleUpdateExpenseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleSearchExpenseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleCancelSearchExpenseButtonClick()
        {
            throw new NotImplementedException();
        }

        public void HandleHoursButtonClick(int memberId)
        {
            foreach (ResearchProject project in this._model)
            {
                if (project.Team.Id.ToString() == this._view.TextBoxTeamId)
                {
                    foreach (ResearchTeamMember member in project.Team.Members)
                    {
                        if (member.Id == memberId)
                        {
                            member.HoursCount = Convert.ToInt32(this._view.TextBoxHours);
                            return;
                        }
                    }
                }
            }
        }

        public void RefreshTeamMembersList()
        {
            foreach (ResearchProject project in this._model)
            {
                if (project.Team.Id.ToString() == this._view.TextBoxTeamId)
                {
                    this._view.PopulateTeamMemberList(project.Team.Members);
                    return;
                }
            }
        }

        public void RefreshPhaseList()
        {
            foreach (ResearchProject project in this._model)
            {
                if (project.Id.ToString() == this._view.TextBoxProjectId)
                {
                    this._view.PopulatePhaseList(project.Phases);
                    return;
                }
            }
        }

        public void RefreshActivitiesList()
        {
            foreach (ResearchProject project in this._model)
            {
                if (project.Id.ToString() == this._view.TextBoxProjectId)
                {
                    foreach (ResearchPhase phase in project.Phases)
                    {
                        if (phase.Id.ToString() == this._view.TextBoxPhaseId)
                        {
                            this._view.PopulateActivitiesList(phase.Activities);
                            return;
                        }
                    }
                }
            }
        }

        public void RefreshExpensesList()
        {
            foreach (ResearchProject project in this._model)
            {
                if (project.Id.ToString() == this._view.TextBoxProjectId)
                {
                    foreach (ResearchPhase phase in project.Phases)
                    {
                        if (phase.Id.ToString() == this._view.TextBoxPhaseId)
                        {
                            foreach (ResearchActivity activity in phase.Activities.Values)
                            {
                                if (activity.Id.ToString() == this._view.TextBoxActivityId)
                                {
                                    this._view.PopulateExpensesList(activity.Budget.Items);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
